package com.scf.loan.controller;

import com.scf.loan.Service.BorrowingsService;
import com.scf.loan.entity.*;
import com.scf.uitl.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/loan")

public class BorrowingsController {
	@Resource
	private BorrowingsService borrowingsService;

	// 查询所有合同
	@PostMapping("/allAsset")
	public ResponseResult allAsset(@RequestBody PageBean pageBean) {
		ResponseResult responseResult = new ResponseResult();
		List<Borrowings> borrowings = borrowingsService.allAsset(pageBean);

		pageBean.setBeanList(borrowings);
		pageBean.setTotalRecord(borrowingsService.counts(pageBean));
		responseResult.setCode(200);
		responseResult.setData(pageBean);
		System.out.println(responseResult);

		return responseResult;
	}

	// 	// 通过id 查询合同信息
	@PostMapping("/getInfoByBoid")
	public ResponseResult getInfoByBoid(@RequestBody Borrowers borrowers) {
		ResponseResult responseResult = new ResponseResult();
		Borrowers infoByBoid = borrowingsService.getInfoByBoid(borrowers);
		if (infoByBoid == null) {
			responseResult.setCode(500);
			responseResult.setMessage("未查找到相关信息");
			return responseResult;
		} else {
			responseResult.setCode(200);
			responseResult.setData(infoByBoid);
			return responseResult;
		}
	}

	// 通过boid 查询银行信息
	@PostMapping("/getBankByBoid")
	public ResponseResult getBankByBoid(@RequestBody Bank bank) {
		ResponseResult responseResult = new ResponseResult();
		List<Bank> bankByBoid = borrowingsService.getBankByBoid(bank);
		responseResult.setCode(200);
		responseResult.setData(bankByBoid);

		return responseResult;
	}

	// 查询所有审批历史
	@GetMapping("/allHistroy")
	public ResponseResult allHistroy( Integer plid) {
		ResponseResult responseResult = new ResponseResult();

		List<History> histories = borrowingsService.allHistory(plid);
		responseResult.setCode(200);
		responseResult.setData(histories);
		return responseResult;
	}

//	根据id 修改合同审核状态
	@PostMapping("/risksub")
	public ResponseResult risksub(@RequestBody Borrowings borrowings){
		System.out.println("bo:  "+borrowings);
		ResponseResult responseResult = new ResponseResult();
		responseResult.setData(borrowingsService.risksub(borrowings));
		System.out.println(responseResult);
		return  responseResult;
	}

	//	更新审批历史
	@PostMapping("/inserthis")
	public ResponseResult inserthis(@RequestBody History history){
		ResponseResult responseResult = new ResponseResult();


		boolean inserthis = borrowingsService.inserthis(history);
		if(inserthis) {
			responseResult.setCode(200);
			responseResult.setMessage("提交成功");
			return responseResult;
		}else{
			responseResult.setCode(500);
			responseResult.setMessage("提交失败");
		}
		return responseResult;
	}
//	返回审批建议
	@GetMapping("/suggestion")
	public ResponseResult suggestion( Integer boid){
		System.out.println("boid: "+boid);
		ResponseResult responseResult = new ResponseResult();
		Borrowings borrowings = new Borrowings();
		Borrowers borrowers = new Borrowers();
		try {
		borrowers.setBoid(boid);
		borrowings.setBwid(boid);
		Borrowings borrowingByBwid = borrowingsService.getBorrowingByBwid(borrowings);
			System.out.println(borrowingByBwid);
		Borrowers infoByBoid = borrowingsService.getInfoByBoid(borrowers);
			System.out.println(infoByBoid);
		// 判断公司资产和借款需求差

			double v = borrowingByBwid.getAmountborrowed() / infoByBoid.getLoanamount();
			if(v>=2){
				responseResult.setMessage("借款需求在正常范围内");
				responseResult.setCode(200);
				return  responseResult;
			}else {
				responseResult.setCode(202);
				responseResult.setMessage("借款需求金额对于该公司较巨大");
				return responseResult;
			}
		}catch (Exception e){
			e.printStackTrace();
			responseResult.setMessage("系统异常！");
			responseResult.setCode(500);
			return responseResult;
		}
	}

}
